package es.usj;

import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class SelectorAlarmasActivity extends Activity {

	Date fechaRecibida = null;
	CheckBox checkBoxEsAlarma = null;
	CheckBox checkBoxRepetir = null;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_selector_alarmas);
        TextView textViewFecha = (TextView) findViewById(R.id.textViewFecha);
        TimePicker timePickerAlarma = (TimePicker) findViewById(R.id.timePickerAlarma);
        checkBoxEsAlarma = (CheckBox) findViewById(R.id.checkBoxEsAlarma);
		Bundle bundle = getIntent().getExtras();
		long fecha = bundle.getLong("fecha", System.currentTimeMillis());
		if (bundle != null){
			fecha = bundle.getLong("fecha", System.currentTimeMillis());
		}
		fechaRecibida = new Date(fecha);
		Calendar calendario = Calendar.getInstance();
		calendario.setTimeInMillis(fecha);
	    timePickerAlarma.setIs24HourView(true);
		timePickerAlarma.setCurrentHour(calendario.get(Calendar.HOUR_OF_DAY));
	    timePickerAlarma.setCurrentMinute(calendario.get(Calendar.MINUTE) );
	    textViewFecha.setText("Fecha: " + android.text.format.DateFormat.format("dd/MM/yyyy", fechaRecibida).toString());

		String descripcion = bundle.getString("descripcion");
		if (descripcion != null && descripcion.trim().length() > 0) {
	        EditText editTextDescripcion = (EditText) findViewById(R.id.editTextDescripcion);
	        editTextDescripcion.setText(descripcion);
		}

		Boolean repetir = bundle.getBoolean("repetir");
		if (repetir != null && repetir == true) {
	       checkBoxRepetir = (CheckBox) findViewById(R.id.checkBoxRepetir);
	       checkBoxRepetir.setChecked(repetir);
		}
		Boolean esAlarma = bundle.getBoolean("esAlarma");
		if (esAlarma != null && esAlarma == true) {
	       checkBoxEsAlarma = (CheckBox) findViewById(R.id.checkBoxEsAlarma);
	       checkBoxEsAlarma.setChecked(esAlarma);
		}

		Button botonOk = (Button) findViewById(R.id.buttonOK);
		Button botonCancel = (Button) findViewById(R.id.buttonCancel);
		Button botonDesconectar = (Button) findViewById(R.id.buttonDesconectar);

		botonOk.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// OK
		        TimePicker timePickerAlarma = (TimePicker) findViewById(R.id.timePickerAlarma);
		        EditText editTextDescripcion = (EditText) findViewById(R.id.editTextDescripcion);
		        checkBoxEsAlarma = (CheckBox) findViewById(R.id.checkBoxEsAlarma);
		        checkBoxRepetir = (CheckBox) findViewById(R.id.checkBoxRepetir);

		        Calendar calendarioTmp = Calendar.getInstance();
				calendarioTmp.setTimeInMillis(fechaRecibida.getTime());
				calendarioTmp.set(Calendar.HOUR_OF_DAY, timePickerAlarma.getCurrentHour());
				calendarioTmp.set(Calendar.MINUTE, timePickerAlarma.getCurrentMinute());
				
		        Intent resultIntent = new Intent();
				resultIntent.putExtra("fecha", calendarioTmp.getTimeInMillis());
				resultIntent.putExtra("minutos", timePickerAlarma.getCurrentMinute());
				resultIntent.putExtra("horas", timePickerAlarma.getCurrentHour());
				if (editTextDescripcion != null && editTextDescripcion.getText() != null
						&& editTextDescripcion.getText().toString().trim().length() > 0) {
					resultIntent.putExtra("descripcion", editTextDescripcion.getText().toString().trim());
				}
				resultIntent.putExtra("repetir", checkBoxRepetir.isChecked());
				resultIntent.putExtra("tiempoRepeticion", 0);
				resultIntent.putExtra("unidadRepeticion", 0);
				resultIntent.putExtra("esAlarma", checkBoxEsAlarma.isChecked());
				setResult(Activity.RESULT_OK, resultIntent);
				finish();
			}
		});

		botonDesconectar.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// BORRA ALARMA
		        Intent resultIntent = new Intent();
				resultIntent.putExtra("fecha", fechaRecibida.getTime());
				setResult(CalendarioFragment.RESULT_DELETE, resultIntent);
				finish();
			}
		});

		botonCancel.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Cancel
				Intent resultIntent = new Intent();
				setResult(Activity.RESULT_CANCELED, resultIntent);
				finish();
			}
		});
		
    }
}
